package 数据流中的第K大元素703;

import java.util.PriorityQueue;

/**
 * @author 谢阳
 * @version 1.8.0_131
 */
class KthLargest {
    int k;
    PriorityQueue<Integer> heap;
    public KthLargest(int k, int[] nums) {
        this.k  = k;
        heap = new PriorityQueue<>();
        for (int num : nums) {
            add(num);
        }
    }

    public int add(int val) {
        if (heap.size() < k || heap.peek() < val) {
            heap.offer(val);
        }

        if (heap.size() > k) {
            heap.poll();
        }
        return heap.peek();
    }
}
